<?php
defined('IN_TS') or die('Access Denied.');

$askid = intval($_GET['id']);
$questionstr = $new['ask']->find('ask_topic', array('askid'=>$askid));
if (!$questionstr || $questionstr['isaudit']==1)
{
	header("HTTP/1.1 404 Not Found");
	header("Status: 404 Not Found");
	$title = '404';
	include pubTemplate("404");
	exit;
}

$questionstr['cate'] = $new['ask']->db->fetch_all_assoc("select catename,a.cateid from ".dbprefix."ask_topic_cate a left join ".dbprefix."ask_cate b on a.cateid=b.cateid where askid=".$askid);
$questionstr['user'] = aac('user')->getOneUser($questionstr['userid']);
$questionstr['addtime'] = date('Y-m-d H:i:s', $questionstr['addtime']);
$questionstr['title'] = tsTitle($questionstr['title']);
$questionstr['content'] = tsDecode($questionstr['content']);

$title = $questionstr['title'];


if($questionstr['tags']){
    $arrTag = explode(',',$questionstr['tags']);
}


//问题添加讨论
$askaddarr = $new['ask']->findAll('ask_question_add', array('askid'=>$askid));
foreach ($askaddarr as $key=>$val)
{
	$adduserarr = aac('user')->getOneUser($val['userid']);
	$askaddarr[$key]['username'] = $adduserarr['username'];
    $askaddarr[$key]['content'] = tsTitle($val['content']);
	$askaddarr[$key]['addtime'] = date('Y-m-d', $askaddarr[$key]['addtime']);
}

//回复列表
$orderflag = '';
$orderby = ' order by digg - undigg desc,a.addtime asc';
if (isset($_GET['orderby']) && $_GET['orderby'] == 'date')
{
	$orderby = ' order by a.addtime asc';
	$orderflag = 'date';
}
$sql = "select a.commentid,a.comment,a.digg,a.undigg,a.addtime,b.userid,b.username,b.face,b.path from ".dbprefix."ask_comment a left join ".dbprefix."user_info b on a.userid=b.userid where a.askid =".$askid.$orderby." limit 0,".COMMENT_PAGE_NUM;
$commentarr = $new['ask']->db->fetch_all_assoc($sql);

//遍历答案
foreach ($commentarr as $key=>$v)
{
	$diggarr = array();
	$oparr = $new['ask']->findall('ask_comment_op', array('commentid'=>$v['commentid']), 'updatetime desc');
	$i = 0;
	foreach ($oparr as $k=>$value)
	{
		if ($value['type'] == 1)
		{
			$tempuser = aac('user')->getOneUser($value['userid']);
			$diggarr[$i]['userid'] = $tempuser['userid'];
			$diggarr[$i]['username'] = $tempuser['username'];
			$i++;
		}
	}

	$commentarr[$key]['diggarr'] = $diggarr;
	
	//问题添加讨论
	$commentaddarr = $new['ask']->findAll('ask_comment_add', array('commentid'=>$v['commentid']));
	if (!$commentaddarr)
	{
		$commentaddarr = array();
	}
	foreach ($commentaddarr as $k=>$val)
	{
		$adduserarr = aac('user')->getOneUser($val['userid']);
		$commentaddarr[$k]['username'] = $adduserarr['username'];
		$commentaddarr[$k]['content'] = tsTitle($val['content']);
		$commentaddarr[$k]['addtime'] = date('Y-m-d', $commentaddarr[$k]['addtime']);
	}
	$commentarr[$key]['askaddarr'] = $commentaddarr;
	$commentarr[$key]['comment'] = tsDecode($v['comment']);
	
}

//如果是登录用户 判断是否回答过该问题
if ($TS_USER['userid'])
{
	$selfcommentarr = $new['ask']->find('ask_comment', array('userid'=>$TS_USER['userid'], 'askid'=>$askid));
	if ($selfcommentarr)
	{
		$notallowcomment = true;
		$selfcomment = tsDecode($selfcommentarr['comment']);
		$originalcomment = tsDecode($selfcommentarr['comment']);
		$selfcommentid = $selfcommentarr['commentid'];
	}
}

$arrAsk = $new['ask']->findAll('ask_topic',array(
    'isaudit'=>0,
),'addtime desc','askid,title,count',10);

$sitekey = $questionstr['tags'];
include template('question');